package com.foreveross.ccjd;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;

import com.foreveross.ccjd.utility.Utils;

public class LoginActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		findViewById(R.id.btn_login).setOnClickListener(this);
		findViewById(R.id.btn_forget_pwd).setOnClickListener(this);
		TextView passwordTv = (TextView) findViewById(R.id.et_password);
		passwordTv.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {

				if (KeyEvent.KEYCODE_ENTER == keyCode
						&& event.getAction() == KeyEvent.ACTION_DOWN) {
					onClick(findViewById(R.id.btn_login));
					return true;
				}
				return false;
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			doLogin();
			break;
		case R.id.btn_forget_pwd:

			break;

		default:
			super.onClick(v);
			break;
		}
	}

	private void doLogin() {
		TextView usernameTv = (TextView) findViewById(R.id.et_username);
		TextView passwordTv = (TextView) findViewById(R.id.et_password);
		String username = usernameTv.getText().toString();
		if (TextUtils.isEmpty(username)) {
			Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
			usernameTv.startAnimation(shake);
			Toast.makeText(this, usernameTv.getHint(), Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (!Utils.isPhoneNum(username)) {
			Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
			usernameTv.startAnimation(shake);
			Toast.makeText(this,R.string.tip_account_error, Toast.LENGTH_SHORT)
			.show();
			return;
		}
		String password = passwordTv.getText().toString();
		if (TextUtils.isEmpty(password)) {
			Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
			passwordTv.startAnimation(shake);
			Toast.makeText(this, passwordTv.getHint(), Toast.LENGTH_SHORT)
					.show();
			return;
		}

	}

	@Override
	protected int getContentView() {
		return R.layout.login;
	}
}
